import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)
const Home = () => import('@pages/Home')
const Login = () => import('@pages/Login')
const Register = () => import('@pages/Register')
const Search = () => import('@pages/Search')
const Detail = () => import('@pages/Detail')
const AddCartSuccess = () => import('@/pages/AddCartSuccess')
const ShopCart = () => import('@/pages/ShopCart')
const Pay = () => import('@/pages/Pay')
const PaySuccess = () => import('@/pages/PaySuccess')
const Center = () => import('@/pages/Center')
const Trade = () => import('@/pages/Trade')

const lastPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location, onComplete = () => {}, onAbort = () => {}) {
    return lastPush.call(this, location, onComplete, onAbort)
}

const router = new VueRouter({
    //控制滚动条，路由跳转，滚动条在顶部
    scrollBehavior() {
        return {
            x: 0,
            y: 0,
        };
    },
    mode: 'history',
    routes: [{
            path: '/home',
            component: Home,
            name: 'home'
        },
        {
            path: '/login',
            component: Login,
            name: 'login',
            meta: {
                footerIsHidden: true
            },
            beforeEnter(to,from,next){
                const token = store.state.user.token
                if(token){
                    next('/')
                }else{
                    next()
                }
            }
        },
        {
            path: '/register',
            component: Register,
            name: 'register',
            meta: {
                footerIsHidden: true
            }
        },
        {
            path: '/search/:keyword?',
            component: Search,
            name: 'search'
        },
        {
            path: '/detail/:skuId',
            component: Detail,
            name: 'detail'
        },
        {
            path: '/addcartsuccess',
            component: AddCartSuccess,
            name: 'addcartsuccess',
            beforeEnter(to,from,next){
                if(sessionStorage.getItem('cartData')){
                    next()
                }else{
                    next('/shopcart')
                }
            }
        },
        {
            path: '/shopcart',
            component: ShopCart,
            name: 'shopcart'
        },
        {
            path: '/pay',
            component: Pay,
            name: 'pay'
        },
        {
            path: '/paysuccess',
            component: PaySuccess,
            name: 'paysuccess'
        },
        {
            path: '/center',
            component: Center,
            name: 'center'
        },
        {
            path: '/trade',
            component: Trade,
            name: 'trade'
        },
        {
            path: '/',
            redirect: '/home'
        }
    ]
})

//权限控制
router.beforeEach((to, from, next) => {
    //需要权限的
    const checkPath = ["/center", "/pay", "/paysuccess", "/trade", "/shopcart"]
    //要去的路由需不需要权限
    const ischeckPath = checkPath.some((item) => {
        return item.startsWith(to.path)
    })

    const token = store.state.user.token
    const nickName = store.state.user.nickName
    //是否有用户名
    if (nickName) {
        return next()
    }

    //是否需要权限
    if (ischeckPath) {
        //是否有token
        if (token) {
            store.dispatch('user/getUserInfo')
            next()
        } else {
            next('/login')
        }
    } else {
        if (token) {
            store.dispatch('user/getUserInfo')
        }
        next()
    }
})
export default router